package com.stylefeng.guns.rest.modular.viewrecord.controller;

import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.RecordListRequest;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.ViewRecordRequest;
import com.stylefeng.guns.rest.modular.viewrecord.service.ViewRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by ASUS-PC on 2018/6/28.
 */
@RestController
@RequestMapping("/viewRecord")
public class ViewRecordController {

    @Autowired
    ViewRecordService viewRecordService;


    //删除观看记录
    @RequestMapping(value = "/deleteRecord", method = RequestMethod.POST)
    public ResponseEntity<?> queryCatalogyList(@RequestBody ViewRecordRequest request) {
        if(request.getRecordId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        boolean state = viewRecordService.deleteRecord(request.getRecordId());
        if(state){
            return ResponseEntity.ok(CommonResponse.ok(null));
        }
        throw new BussinessException(BizExceptionEnum.UNKNOWN_ERROR);
    }

    //获取用户观看记录列表
    @RequestMapping(value = "/viewRecordList", method = RequestMethod.POST)
    public ResponseEntity<?> viewRecordList(@RequestBody RecordListRequest request) {
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        return ResponseEntity.ok(CommonResponse.ok(viewRecordService.queryViewRecords(request)));

    }

}
